package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * RestController注解:
 *   1.组合注解,等价于: @Controller注解 +  @ResponseBody注解;
 *   2.添加此注解后,所有的控制器方法上不需要再添加 @ResponseBody注解了;
 * Slf4j注解:
 *   1.Lombok提供的日志注解;
 *   2.添加在类上,会为该类创建一个日志对象log;
 * Knife4j注解:
 *   1.@Api注解: 类上,调整控制器类名在Knife4j文档中的显示名称;
 *   2.@ApiOperation注解: 方法上,调整控制器方法名在Knife4j文档中的显示名称;
 *   3.@ApiOperationSupport注解: 方法上,方法在文档中的显示顺序;
 *   4.@ApiModelProperty注解: POJO类属性上;
 *   5.@ApiImplicitParam注解: 非封装单个参数描述;
 *   6.@ApiImplicitParams注解: 非封装多个参数描述;
 *   7.@ApiIgnore注解: 忽略某个参数;
 */
@Slf4j
@RestController
@Api(tags = "01.通知管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    //自动装配
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 1.添加通知功能
     * @param noticeAddParam
     * @return
     */
    @ApiOperation("添加通知")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(NoticeAddParam noticeAddParam){
        /*
            以注册功能为例[梳理业务]:
            1.接收客户端传递的数据;
            2.校验数据的合法性[暂时不校验];
            3.校验用户名是否被占用[SELECT ...];
              3.1 被占用,return结束;
              3.2 未被占用,在数据表中插入数据[INSERT ...];
         */
        log.debug("noticeAddParam = " + noticeAddParam);
        //调用接口方法,在notice表中插入1条数据
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        //单独设置其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice = " + notice);
        //调用mapper层接口,插入数据
        noticeMapper.insert(notice);

        return JsonResult.ok();
    }

    /**
     * 2.通知列表
     */
    @ApiOperation("通知列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult list(NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery = " + noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();
        //调用接口
        List<NoticeListVO> noticeListVOS = noticeMapper.noticeList(title, type);

        return JsonResult.ok(noticeListVOS);
    }

    /**
     * 3.公告详情功能
     */
    @ApiOperation("公告详情")
    @ApiOperationSupport(order = 30)
    @GetMapping("detail")
    @ApiImplicitParam(name = "id", value = "通知编号", required = true, dataType = "int")
    public JsonResult detail(Integer id, @ApiIgnore HttpSession session){
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);

        return JsonResult.ok(noticeDetailInfoVO);
    }

    /**
     * 4.删除公告功能
     */
    @ApiOperation("删除公告")
    @ApiOperationSupport(order = 40)
    @PostMapping("del")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "通知编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "title", value = "通知标题", required = true, dataType = "string")
    })
    public JsonResult delete(Integer id, String title){
        //title参数只是为了knife4j测试,没有其他作用
        log.debug("id = " + id);
        int i = noticeMapper.delete(id);
        if (i > 0){
            return JsonResult.ok();
        }

        //该公告不存在
        return new JsonResult(StatusCode.NOTICE_NOT_EXISTS);
    }

    /**
     * 5.更新公告功能
     */
    @ApiOperation("更新公告")
    @ApiOperationSupport(order = 50)
    @PostMapping("update")
    public JsonResult update(NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        noticeMapper.update(notice);

        return JsonResult.ok();
    }
}











